import { defineStore } from 'pinia'
import router from '@/router'

export const useUserStore = defineStore('user-store', {
  state: () => {
    return {
      token: localStorage.getItem('token') || null,
    }
  },
  actions: {
    setUserState (token) {
      token = 'Bearer '+token
      this.token = token
      localStorage.setItem('token', token)
    },
    logout () {
      localStorage.removeItem('token')
      router.replace({ name: 'login' }).finally(async () => {
        $message.destroyAll()
        this.$patch({ token: '' })
        location.reload()
      })
    },
  },
})
